<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑差异处理</title>
    <link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
    <style>
		a.back{color:#ff6a08!important}
		.layui-input{width:140px;}
		.layui-input-long{width:425px;}
		.layui-form-item .layui-input-inline{width: 140px;}
		.inputdiv{display:flex;height: 38px;line-height: 38px;}
		.inputdiv i{position:absolute;right:10px;}
		.sheetno{margin-left:20px;}
		.approve{position: absolute;filter: alpha(opacity=100,Style=0);width: 150px;border-style: solid;border-width: 2px;border-color: red;
		    word-break: break-all;table-layout: fixed;font-family: 隶书;color: Red;font-size: 30px;padding-left: 5px;
		    padding-top: 5px;padding-bottom: 5px;padding-right: 5px;text-align: center;left: 400px;top:80px;z-index:1000;}
	</style>
</head>
<body>

		<div class="layui-card">
			<div class="layui-card-header">
				<button class="pear-btn pear-btn-primary pear-btn-md" onclick="backToList()">
				<i class="pear-icon pear-icon-back"></i>
				返回
				</button>
				{if condition="$one['approve'] neq '1'"}
				<button class="pear-btn pear-btn-primary pear-btn-md" onclick="saveCombItem()">
				<i class="pear-icon pear-icon-save"></i>
				保存并返回
				</button>
				{present name="one"}
				{if condition="$one['approve'] neq '1' and $one['sheetno'] neq ''"}
				<button class="pear-btn pear-btn-warming pear-btn-md" onclick="approve()">
				<i class="layui-icon layui-icon-vercode"></i>
				审核
				</button>
				{/if}
				{/present}
				{/if}
				
				{if condition="$one['sheetno'] neq ''"}
				<label class="sheetno">单号:【{$one.sheetno}】</label>
				{/if}
				{if condition="$one['approve'] eq '1'"}
				<label class="approve">【已审核】</label>
				{/if}
				
			</div>
		</div>

		<div class="layui-card">
					<form class="layui-form" action="" onsubmit="return false">
						<input type="hidden" id="hidShowSheetNo" name="hidShowSheetNo" value="{$one.sheetno}" />
						<div class="layui-container">  
						  <div class="layui-row">
						    
					    		 <div class="layui-form-item">
					    		 	<div class="layui-form-item layui-inline">
									    <label class="layui-form-label">盘点批号</label>
									    <div class="layui-input-inline inputdiv">
									    	<input type="text" readonly="readonly" name="pdsheetno" id="pdsheetno" autocomplete="off" value="{$one['sheetno']}" placeholder="" class="layui-input">
											<i class="pear-icon pear-icon-survey"></i>
										</div>
								    </div>
					    		 	 <div class="layui-form-item layui-inline">
										<label class="layui-form-label">制单人员</label>
										<div class="layui-input-inline">
											<input type="text" readonly="readonly" name="order_man" id="order_man" value="{$orderMan}" autocomplete="off" placeholder="" class="layui-input" readonly="readonly">
										</div>
									</div>
									
									<div class="layui-form-item layui-inline">
										<label class="layui-form-label">制单日期</label>
										<div class="layui-input-inline">
											<input type="text" readonly="readonly" name="oper_date" id="oper_date" value="{$one['operDate']?$one['operDate']:date('Y-m-d H:i:s',time())}" autocomplete="off" placeholder="" class="layui-input">
										</div>
									</div>
								  </div>
								  
								   <div class="layui-form-item">
									   <div class="layui-form-item layui-inline">
										    <label class="layui-form-label">仓库</label>
										    <div class="layui-input-inline inputdiv">
										    	<input type="text" readonly="readonly" name="branch_no" id="branch_no" autocomplete="off" value="{$one['branchNo']}" placeholder="" class="layui-input" readonly="readonly">
												<input type="hidden" id="branch_name" name="branch_name" value="{$one['branchName']}" />
												<i class="layui-icon layui-icon-home"></i>
											</div>
									    </div>
									    
									    <div class="layui-form-item layui-inline">
										    <label class="layui-form-label">盘点范围</label>
										    <div class="layui-input-inline inputdiv">
										    	<input type="text" readonly="readonly" name="oper_range" id="oper_range" autocomplete="off" value="{$one['operRange']}" placeholder="" class="layui-input" readonly="readonly">
											</div>
									    </div>
						    		 	
									    <div class="layui-form-item layui-inline">
											<label class="layui-form-label">备注原因</label>
											<div class="layui-input-block">
												<input type="text" readonly="readonly" name="memo" id="memo" value="{$one.memo}" autocomplete="off" placeholder="" class="layui-input">
											</div>
										</div>
								   
								  </div>
						  </div>
						  
						</div>
					</form>
		</div>
		  
 		<div class="layui-card">
			<div class="layui-card-body">
				<table id="difsheet-table" lay-filter="difsheet-table"></table>
			</div>
		</div>
		
		<script type="text/html" id="comb-bar">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
			<i	class="layui-icon layui-icon-delete"></i>
			</button>
	    </script>     

<script src="__STATIC__/lib/layui/layui.js"></script>
<script src="__PEAR__/component/pear/pear.js"></script>
<script src="__STATIC__/js/admin/common.js"></script>
<script>
var $;
var _layerIndex=null;
var all_item_no=[];
var rowIndex=0;
var rowObj=null;
var jq=null;
var branchNo='';
var pdsheetno="";
var isSingle=false;
var formulaChecked=2;
var stockflag=0;
var ajaxing=false;
var loadGood=false;

//判断是否未选择分店
function emptyOptions(){
	pdsheetno=$("#pdsheetno").val();
	if(pdsheetno==''){
			layer.alert("请选择盘点批号");
			return false;
	}
	return true;
}

layui.use(['form','jquery','table','common','laydate'],function(){
    let form = layui.form;
    let common = layui.common;
    let table = layui.table;
    let laydate=layui.laydate;
    $ = layui.jquery;
    jq=$;
    
    
    laydate.render({
        elem: '#pay_date'
        ,format: 'yyyy-MM-dd'
      });
    
    
    var path="/{$MODULE_NAME}/check/crsheet/";
    {if condition="$one['sheetno'] neq ''"}
    let	table_url="/{$MODULE_NAME}/check/Jsondata/getPdsheetDetailList/no/{$one['sheetno']}/rangeno/{$one['oper_range']}/branchno/{$one['branchNo']}";
	{else/}
	let	table_url="";
	{/if}

	let cols = [
		[
			{
				title: '行号',
				field: 'rowIndex',
				align: 'center',
				width: 80,
				fixed: 'left'
			},
			{
				title: '商品货号',
				field: 'item_no',
				align: 'center',
				width: 180,
				fixed: 'left'
			},
			{
				title: '商品名称',
				field: 'item_name',
				align: 'center',
				width: 180,
				fixed: 'left'
			},
			{
				title: '自编码',
				field: 'item_subno',
				align: 'center',
				width: 180
			},
			{
				title: '库存数量',
				field: 'item_stock',
				align: 'center',
				width: 100
			},
			{
				title: '实际盘点数量',
				field: 'check_qty',
				align: 'center',
				width: 150
			},
			{
				title: '盈亏数量',
				field: 'balance_qty',
				align: 'center',
				width: 150
			},
			{
				title: '备注',
				field: 'memo',
				align: 'center',
				style:'background-color: #fff6f0;',
				edit:'text',
				width: 150
			},
			{
				title: '单位',
				field: 'item_unit',
				align: 'center',
				width: 100
			},
			{
				title: '规格',
				field: 'item_size',
				align: 'center',
				width: 100,
			},
			{
				title: '进货规格',
				field: 'purchase_spec',
				align: 'center',
				width: 100,
			},
			{
				title: '进价',
				field: 'item_price',
				align: 'center',
				width: 100,
			},
			{
				title: '零售价',
				field: 'sale_price',
				align: 'center',
				width: 100,
			},
			{
				title: '售价金额',
				field: 'sale_amt',
				align: 'center',
				width: 100
			}
			
		]
	]

	table.render({
		elem: '#difsheet-table',
		url: table_url,
		page: false,
		cols: cols,
		limit:1000,
		skin: 'line',
		toolbar: '',
		defaultToolbar: [],
		done:function(){
			
			{if condition="$one['approve'] neq '1'"}
			editListener();
			{else /}
			limitEdit();
			{/if}
		}
	});
	
	//重载表格
	window.reloadTable=function(rows){
		table.reload('difsheet-table',{
			url: '',
			data : rows
		});
	}
    
    //监听单元格编辑
  	table.on('edit(difsheet-table)', function(obj){
  		
    	if(!emptyOptions()){
    		$(obj.tr.selector + ' td[data-field="' + obj.field + '"] input').val("");
    		return;
    	}
    	
  		//验证数字规则
  		var regNum =  /^[0-9.]*$/g;
  		//验证商品编码
  		var regNo =  /^[0-9a-zA-Z]*$/g;
  		
  		var value = obj.value //得到修改后的值
		    ,data = obj.data //得到所在行所有键值
		    ,field = obj.field; //得到字段
	    var selector = obj.tr.selector+' td[data-field="'+obj.field+'"] div';   
		var oldtext = $(selector).text();

		 if(field=='item_no'&&!regNo.test(value)){
	    		layer.alert("请输入字母或数字");
	    	    $(obj.tr.selector + ' td[data-field="' + obj.field + '"] input').val(oldtext);
	    		return;
		 }
		 
		 if(field=='check_qty'){
			 if(!regNum.test(value)){
		    		layer.alert("请输入数字");
		    	    $(obj.tr.selector + ' td[data-field="' + obj.field + '"] input').val(oldtext);
		    		return;
	    	}
		 }    
		 
		 //修改当前行的值，刷新表格
		 if(field=='check_qty'){
			 	var index=obj.tr.attr("data-index");
				var rows=table.getData("difsheet-table");
					//修改多个字段值
					data=window.sumTotal(data);
					rows[index]=data;
					//重新加载表格
					reloadTable(rows);
					return;
		 }
		 
		 rowIndex=obj.tr.attr("data-index");
		 rowIndex=parseInt(rowIndex);
  		if(field=='item_no'&&$.trim(value)!=''){
  			
  			loadGood=true;
  			//判断商品编码，并且弹出商品选择窗口
	    	$.ajax({
				url:"/{$MODULE_NAME}/common/index/getiteminstance",
				data:{"itemno":value},
				dataType: 'json',
				type: 'POST',
				success: function(result) {
					//如果不为空，则填充数据
					if(result.code==1){
						
							var tempData=result.data;
								tempData['rowIndex']=rowIndex+1;
								tempData=copyField(tempData);
			
								window.editRow(rowIndex,tempData);
					}else{
							//弹出商品选择窗口
							selectSingleGoods("");
					}
					
					loadGood=false;
				}
			})
			
  		}	
  	});
    
	//返回
	window.backToList=function(){
		window.location.href="/{$MODULE_NAME}/check/Crsheet/dfindex";
	}
	
	//导出报表
	window.exportxls=function(){
		window.location.href="/{$MODULE_NAME}/check/Crsheet/exportPdsheet/no/"+ $("#hidShowSheetNo").val();
	}
	
	//监听单元格编辑
	function editListener(){
	    //数据表格点击事件
	    table.on('row(difsheet-table)', function(obj){
			
	    	if(loadGood){
	    		return;
	    	}
	    	
	    	var bg=$(this).attr("style");
	    	if(bg==undefined||bg=='background-color:#ffffff;'){
	    		$(this).attr({ "style": "background-color:#fafafa;" });
	    	}else{
	    		$(this).attr({ "style": "background-color:#ffffff;" });
	    	}
	    	
	    	if(!emptyOptions()){
	    		return;
	    	}
	    	
	    	rowObj=obj;
			//当前编辑行的序号
			rowIndex=rowObj.tr.attr("data-index");

		});
	}
	
	//审核盘点批号
	window.approve=function(){
		
		//防止多次触发
		if(ajaxing){
			return;
		}
		
		var sheetno=$("#pdsheetno").val();
		if($.trim(sheetno)==''){
			layer.msg("缺少盘点批号");
			return;
		}
		
		var rows=table.getData("difsheet-table");
		
		if(rows.length<=0){
			layer.msg("缺乏商品明细");
			return;
		}

		
		layer.confirm('确认审核'+sheetno+'单据?', {
			icon: 3,
			title: '提示'
		}, function(index) {
			$.ajax({
	            url:path+"approve",
	            data:{"sheetno":sheetno,"items":rows,"rangeno":"{$one['oper_range']}"},
	            dataType:'json',
	            type:'post',
	            success:function(result){
	                if(result.code){
	                	
	                	layer.msg(result.msg, {
							icon: 1,
							time: 2000
						}, function() {
							location.reload();
						});

	                }else{
	                    layer.msg(result.msg,{icon:2,time:2000});
	                }
	                
	                ajaxing=false;
	            }
	     	})
		});

	}
	
	//保存
	window.saveCombItem=function(){
		
		//防止多次触发
		if(ajaxing){
			return;
		}
		
    	if(!emptyOptions()){
    		return;
    	}
		
		var lindex=layer.msg("请稍后...");
		var rows=table.getData("difsheet-table");
		
		if(rows.length<=0){
			layer.msg("请输入商品明细");
			return;
		}
		
		var hasGood=false;
		for(var i in rows){
			if(rows[i]['item_no']!=''&&rows[i]['item_no']!=undefined){
				hasGood=true;
				break;
			}
		}
		
		if(!hasGood){
			layer.msg("请输入商品明细");
			return;
		}
        
		var _data={
			        "sheetno": $("#pdsheetno").val(),//盘点申请批号
			        "items":rows
				}
		
		ajaxing=true;
        
		$.ajax({
	            url:path+"dfsave",
	            data:_data,
	            dataType:'json',
	            type:'post',
	            success:function(result){
	                if(result.code){
	                	layer.close(lindex);
	                	
	                	layer.msg(result.msg, {
							icon: 1,
							time: 2000
						}, function() {
							window.backToList();
						});

	                }else{
	                    layer.msg(result.msg,{icon:2,time:2000});
	                }
	                
	                ajaxing=false;
	            },
	            error:function(){
	            	layer.msg("服务器返回错误",{icon:2,time:2000});
	            	ajaxing=false;
	            }
	     })
	}
	
})
</script>
</body>
</html>